package ch.electronet.wjw.index;

import java.io.PrintStream;

public class PrintVisitor implements NodeVisitor {

	private final PrintStream out;
	private int level;

	public PrintVisitor() {
		this(System.out);
	}

	public PrintVisitor(PrintStream out) {
		this.out = out;
		this.level = 0;
	}

	public void visit(Node node) {
		if (!node.isRoot()) {
			printTabs();
			level++;
			out.print("- ");
			out.print((char) node.getKey());
			out.println(node.getData());
		}
		for (Node child : node.getChildren()) {
			child.accept(this);
		}
		if (!node.isRoot()) {
			level--;
		}
	}

	private void printTabs() {
		for (int i = 0; i < level; i++) {
			out.print(" ");
		}
	}

}
